package com.netclass.meethealth.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.netclass.meethealth.R;
import com.netclass.meethealth.model.Apply;
import com.netclass.meethealth.model.Sport;

import java.util.ArrayList;
import java.util.List;

/**
 * sport详情申请信息的适配器
 * <p/>
 * Created by luodong on 15/12/27.
 */
public class PartApplyAdapter extends BaseAdapter {

    // 申请信息LIST
    private List<Apply> applyList;
    // context
    private Context context;
    // 是否是发起人
    private boolean isAuthor = false;

    public PartApplyAdapter(Context context, List<Apply> applyList1) {
        this.context = context;
        if (applyList1 != null) {
            applyList = applyList1;
        } else {
            applyList = new ArrayList<>();
        }
        if (applyList1.size() > 1)
            isAuthor = true;
    }

    @Override
    public int getCount() {
        return applyList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_part_apply, null);
            viewHolder.name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.phone = (TextView) convertView.findViewById(R.id.tv_phone);
            viewHolder.tell = (ImageView) convertView.findViewById(R.id.iv_tell);
            viewHolder.msg = (ImageView) convertView.findViewById(R.id.iv_message);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            viewHolder.name.setText(applyList.get(position).getName() + "");
            viewHolder.phone.setText(applyList.get(position).getPhone() + "");
            if (isAuthor) {
                viewHolder.tell.setVisibility(View.VISIBLE);
                viewHolder.msg.setVisibility(View.VISIBLE);
                viewHolder.tell.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.tell(applyList.get(position).getPhone());
                        }
                    }
                });
                viewHolder.msg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.msg(applyList.get(position).getPhone());
                        }
                    }
                });
            } else {
                viewHolder.tell.setVisibility(View.GONE);
                viewHolder.msg.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    /**
     * 刷新所有数据
     *
     * @param applies
     */
    public void refresh(List<Apply> applies) {
        this.applyList = applies;
        notifyDataSetChanged();
    }

    class ViewHolder {
        private TextView name;//申请人姓名
        private TextView phone;//申请人电话
        private ImageView tell;//打电话
        private ImageView msg;//发短信
    }

    private OnApplyClickListener listener;

    /**
     * 设置监听器
     *
     * @param listener
     */
    public void setApplyButtonClickListener(OnApplyClickListener listener) {
        this.listener = listener;
    }

    /**
     * 点击列表项中的按钮的接口
     */
    public interface OnApplyClickListener {
        /**
         * 打电话
         *
         * @param phone
         */
        public void tell(String phone);

        /**
         * 发短信
         *
         * @param phone
         */
        public void msg(String phone);
    }
}
